<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #result{
            width: 300px;
            height:200px;
            border:solid 1px #222;
        }
    </style>
</head>
<body>
    <input type="text"><button>点击查询</button><br>
    <hr>
    <div id="result"></div>

    <script>
        //获取 input 元素与 button 元素
        const input = document.querySelector('input');
        const button = document.querySelector('button');
        const result = document.querySelector('#result');

        //绑定事件
        button.addEventListener('click', function(){
            //获取用户输入的城市名称
            let city = input.value;
            //声明 url 变量
            let url = 'https://www.tianqiapi.com/api/?version=v1&city='+city+'&appid=83381893&appsecret=foUMnGY7'
            //
            const xhr = new XMLHttpRequest();
            xhr.responseType = 'json';
            xhr.open("GET", url);
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    if(xhr.status >= 200 && xhr.status < 300){
                        //打印结果
                        // console.log(xhr.response);
                        let {air_level, air_tips, wea} = xhr.response.data[0];
                        //设置 div 文本
                        result.innerHTML = `今日天气: ${wea}<br><br> 空气质量: ${air_level} <br><br> 小提示: ${air_tips}`;
                    }
                }
            }
        });
        
    </script>
</body>
</html>